﻿using MachineVision.API.Models;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Concurrent;

namespace MachineVision.API.SignalR
{
    public class SignalRHub : Hub//<IClient>
    {
        public static ConcurrentDictionary<string, RobotState> userInfos = new ConcurrentDictionary<string,  RobotState>();

        /// <summary>
        /// 在所有连接的客户端上调用方法
        /// </summary>
        /// <returns></returns>
        [HubMethodName("SendMessage")]
        public async Task SendMessage(string user, RobotState message)
        {
            //var uu = userInfos.Where(s => s.Key == Context.ConnectionId); 
            userInfos[Context.ConnectionId] = message;

            await Clients.Group("Observer").SendAsync("ShowInfo", message);
            await Clients.Caller.SendAsync("ShowMsg", message.Name);
            //await Clients.Client(uid).SendAsync("ShowMsg",message.Name); //等同↑
        }

        [HubMethodName("SendStatement")]
        public async Task SendStatement(string user)
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, "Observer");
            await Clients.Caller.SendAsync("ShowMsg", "加入观察者");
        }

        #region MyRegion
        ///// <summary>
        ///// 在调用集线器方法的客户端上调用方法
        ///// </summary>
        //public async Task SendMessageToCaller(string user, string message)
        //{
        //    //await Clients.Caller.ReceiveMessage(user, message);
        //}

        ///// <summary>
        ///// 在所有连接的客户端上调用方法，但调用方法的客户端除外
        ///// </summary>
        //public async Task SendMessageToOthers(string user, string message)
        //{
        //    //await Clients.Others.ReceiveMessage(user, message);
        //}

        ///// <summary>
        ///// 向组的所有客户端发送消息
        ///// </summary>
        //public async Task SendMessageToGroup(string GroupName, string user, string message)
        //{
        //    ////var item = Context.Items
        //    //await Clients.Group(GroupName).ReceiveMessage(user, message);
        //}

        ///// <summary>
        ///// 向指定用户发送消息
        ///// </summary>
        //public async Task SendMessageToUser(string user, string message)
        //{
        //    var ConnectionId = Context.ConnectionId;
        //    var UserId = userInfos.Where(x => x.Key != ConnectionId).FirstOrDefault().Key;
        //    //await Clients.Client(UserId).ReceiveMessage(user, message);
        //}


        #endregion

        /// <summary>
        /// 处理连接的事件
        /// </summary>
        /// <returns></returns>
        public override async Task OnConnectedAsync()
        {
            //Context.Items.Add()
            var ConnectionId = Context.ConnectionId;
            Console.WriteLine($"{DateTime.Now}-{ConnectionId}-Connect User");
            userInfos.TryAdd(ConnectionId, new RobotState{Name= Guid.NewGuid().ToString() });
            await base.OnConnectedAsync();

            await Clients.All.SendAsync("ShowMsg","Connection succeeded "+ ConnectionId);
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            var ConnectionId = Context.ConnectionId;
            //TODO  outs的逻辑
            var result = userInfos.TryRemove(ConnectionId, out RobotState outs);
            Console.WriteLine($"{DateTime.Now}-{ConnectionId}-Remove User");

            await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Observer");
            await base.OnDisconnectedAsync(exception);
            await Clients.All.SendAsync("ShowMsg", "Disconnect " + ConnectionId);
        }
    }
}
